﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Dapper;

namespace ProMvc.Repository
{
    public class UserRepository
    {
        private string connString;
        public UserRepository()
        {
            connString = ConfigurationManager.ConnectionStrings["test"].ConnectionString;
        }

        public void Add()
        {
            using (var conn = new SqlConnection(connString))
            {
                conn.Open();
                string sql = "insert into Users(UName,AddDate)values(@UName,@AddDate)";
                SqlCommand cmd = conn.CreateCommand();
                SqlParameter[] sqlparams = { 
                                               new SqlParameter() { DbType = DbType.String, Value = "Z", ParameterName = "@UName", Direction = ParameterDirection.Input }, 
                                               new SqlParameter() { DbType = DbType.DateTime, Value = DateTime.Now, ParameterName = "@AddDate", Direction = ParameterDirection.Input } 
                                           };
                cmd.Parameters.AddRange(sqlparams);
                cmd.CommandText = sql;
                int num = cmd.ExecuteNonQuery();
                conn.Close();
            }
        }

        public void OrmAdd()
        {
            using (var conn = new SqlConnection(connString))
            {
                conn.Open();
                string sql = "insert into Users(UName,AddDate)values(@UName,@AddDate)";
                int num = conn.Execute(sql, new { UName = "Glodon",AddDate=DateTime.Now });
                conn.Close();
            }
        }
    }
}